package com.junqson.je.msg.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;

/**
 * @Descriptions 站内信模型
 * @Company
 * @Author Junqson
 * @Date 2020/2/10 17:57
 * @Version 1.0
 */
@Data
@Entity
public class MessageModel {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long msgId;

    private Integer fromId;

    private Integer toId;

    private String title;

    private String content;

    private Boolean readState = false;

    // 会话id fromId和toId组合，小的在前面
    private String conversationId;


    @JsonIgnore
    @UpdateTimestamp
    private Date updateTime;

    @CreationTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;


    public void genConversationId() {
        String fmt = "%d_%d";
        if (fromId < toId) {
            conversationId = String.format(fmt, fromId, toId);
        } else {
            conversationId = String.format(fmt, toId, fromId);
        }
        System.out.println(conversationId);
    }

}